<template>
  <canvas ref="canvas" class="scalable-canvas" />
</template>

<script>
  export default {
    name: 'ScalableCanvas',
    mounted() {
      const canvas = this.$refs.canvas
      const parentEl = this.$parent.$el
      const hwRatio = canvas.offsetHeight / canvas.offsetWidth

      window.addEventListener('resize', () => {
        canvas.style.width = parentEl.offsetWidth + 'px'
        canvas.style.height = parentEl.offsetWidth * hwRatio + 'px'
      })
      canvas.style.width = parentEl.offsetWidth + 'px'
      canvas.style.height = parentEl.offsetWidth * hwRatio + 'px'
    },
    methods: {
      getCanvas() {
        return this.$refs.canvas
      }
    }
  }
</script>

<style scoped>
  .scalable-canvas {
    width: 100%;
    height: 100%;
  }
</style>
